﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sinacor.OMM.ConnectionManager.MSMQ
{
    public class MSMQHelper
    {

        /// <summary>
        /// Constructor
        /// </summary>
        public MSMQHelper()
        { }

        /// <summary>
        /// Create a MSMQ sender repository
        /// </summary>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public OMM.ConnectionManager.MSMQ.MsmqSender CreateRepository(OMM.Configuration.Repository.RepositoryConfiguration configuration)
        {
            //
            OMM.ConnectionManager.MSMQ.MsmqSender repository = null;
            String repositoryPath = String.Empty;
            Int32 queueMessageLimit;
            long queueBytesLimit;

            //
            repositoryPath = configuration.ConnectionInformation[Configuration.Repository.RepositoryPropertiesConstants.ConnectionString];

            if (!Int32.TryParse(configuration.ConnectionInformation[Configuration.Repository.RepositoryPropertiesConstants.QueueMessageLimit], out queueMessageLimit))
                queueMessageLimit = int.MaxValue;

            if (!long.TryParse(configuration.ConnectionInformation[Configuration.Repository.RepositoryPropertiesConstants.QueueBytesLimit], out queueBytesLimit))
                queueBytesLimit = long.MaxValue;

            repository = new MsmqSender(repositoryPath, queueMessageLimit, queueBytesLimit);

            //
            return repository;

        }

        /// <summary>
        /// Create a MSMQ receiver repository
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="logIdentifier"></param>
        /// <returns></returns>
        public OMM.ConnectionManager.MSMQ.MsmqReceiver CreateRepositoryMsmqReceiver(OMM.Configuration.Repository.RepositoryConfiguration configuration)
        {            
            //
            OMM.ConnectionManager.MSMQ.MsmqReceiver repository = null;
            String repositoryPath = String.Empty;

            //
            repositoryPath = configuration.ConnectionInformation[Configuration.Repository.RepositoryPropertiesConstants.ConnectionString];
            repository = new MsmqReceiver(repositoryPath);

            //
            return repository;
        }

    }
}
